<?php

namespace addons\shopro\controller;


use app\common\model\YajinLogModel;
use think\Db;

class Order extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $params = $this->request->get();

        $this->success('订单列表', \addons\shopro\model\Order::getList($params));
    }

    public function detail()
    {
        $params    = $this->request->get();
        $orderInfo = \addons\shopro\model\Order::detail($params);
        if ($orderInfo['status'] == 1 || $orderInfo['status'] == 2) {
            if ($orderInfo['yajin'] > 0) {
                $orderInfo['total_fee'] = $orderInfo['yajin'];
            }

        }
        $this->success('订单详情', $orderInfo);
    }

    public function itemDetail()
    {
        $params = $this->request->get();
        $this->success('订单商品', \addons\shopro\model\Order::itemDetail($params));
    }

    // 即将废弃
    public function statusNum()
    {
        $this->success('订单数量', \addons\shopro\model\Order::statusNum());
    }

    // 取消订单
    public function cancel()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'cancel');

        $this->success('取消成功', \addons\shopro\model\Order::operCancel($params));
    }

    // 删除订单
    public function delete()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'delete');

        $this->success('删除成功', \addons\shopro\model\Order::operDelete($params));
    }

    // 确认收货
    public function confirm()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'confirm');

        $this->success('收货成功', \addons\shopro\model\Order::operConfirm($params));
    }

    public function comment()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'comment');

        $this->success('评价成功', \addons\shopro\model\Order::operComment($params));
    }

    public function pre()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'pre');

        $result = \addons\shopro\model\Order::pre($params);

        if (isset($result['msg']) && $result['msg']) {
            $this->error($result['msg'], $result);
        } else {
            $user_group_info = '';
            $yajin           = 0;
            $is_water_coupon = $params['is_water_coupon'] ?? 0;

            if ($this->auth->group_id) {
                $user_group_info           = Db::name('user_group')->where([
                    'id'     => $this->auth->group_id,
                    'is_vip' => 2
                ])->find();
                $ps_money                  = $user_group_info['ps_money'];
                $result['ps_money']        = $ps_money;
                $result['dispatch_amount'] += $ps_money;
                $result['total_fee']       += $ps_money;
            }
            if (!$is_water_coupon) {
                if (!$this->auth->group_id || !$user_group_info) {
//                    $yajin_num = YajinLogModel::where(['user_id' => $this->auth->id, 'status' => 1])->sum('num') ?? 0;
//                    $goods_num = sumArrayValues($result['new_goods_list'], 'goods_num');
                    $goods_num = $params['goods_num'] ?? 0;
                    if ($goods_num) {
//                        $goods_num    -= $yajin_num;
                        $configModel  = new \addons\shopro\model\Config;
                        $config_value = $configModel->where('name', '=', 'shopro')->value('value');
                        $shoproConfig = json_decode($config_value, true);
                        $yajin        = bcmul($shoproConfig['yajin'], $goods_num, 2);
                    }
                }
            }
            $result['total_fee']    = bcadd($result['total_fee'], $yajin, 2);
            $result['total_amount'] = bcadd($result['total_amount'], $yajin, 2);
            $result['yajin']        = $yajin;
            $this->success('计算成功', $result);
        }
    }

    public function createOrder()
    {
        $params = $this->request->post();
        // 表单验证
        $this->shoproValidate($params, get_class(), 'createOrder');
        $order = \addons\shopro\model\Order::createOrder($params);

        if ($order['create_result'] === 200) {
            $this->success('订单添加成功', $order);
        } else {
            $this->error($order['msg'], []);
        }
    }

    /**
     * @menu:更新
     * @Date:2024-06-28 16:58
     */
    public function orderUp()
    {
        $order_id  = input('order_id');
        $goods_num = input('goods_num');
        if (!$order_id || !$goods_num) {
            $this->error('缺少参数');
        }
        $orderInfo = \addons\shopro\model\Order::where(['id' => $order_id])->find();
        if (!$orderInfo) {
            $this->error('订单不存在');
        }
        $configModel          = new \addons\shopro\model\Config;
        $config_value         = $configModel->where('name', '=', 'shopro')->value('value');
        $shoproConfig         = json_decode($config_value, true);
        $orderInfo->yajin     = bcmul($shoproConfig['yajin'], $goods_num, 2);
        $orderInfo->yajin_num = $goods_num;
        $orderInfo->save();
        $this->success('ok');
    }

    // 获取可用优惠券列表
    public function coupons()
    {
        $params = $this->request->post();

        // 表单验证
        $this->shoproValidate($params, get_class(), 'coupons');

        $coupons = \addons\shopro\model\Order::coupons($params);

        $this->success('获取成功', $coupons);
    }

    public function sendStore()
    {
        $id          = input('ids');
        $stro_id     = input('stor_id');
        $this->model = new \app\admin\model\shopro\order\Order;
        $order       = $this->model->with(['item'])->where('id', $id)->find();
        if (!$order) {
            $this->error('订单不存在或未支付');
        }
        $order->storeTwoOrderSend($order, $stro_id);
        YajinLogModel::where(['order_id' => $id])->update(['store_id' => $stro_id]);
        // 重新获取订单
        $this->success('发货成功', $order);
    }
}
